//
//  BaseViewController.swift
//  TarBarJump
//
//  Created by 郑东喜 on 2016/12/14.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit
import WebKit

var mainIndexArray = NSMutableArray()

func XFLog<T>(message : T, file : String = #file, lineNumber : Int = #line) {
    
    #if DEBUG
        
        let fileName = (file as NSString).lastPathComponent
        print("[\(fileName):line:\(lineNumber)]- \(message)")
        
    #endif
}

class BaseViewController: BaseVC,WKNavigationDelegate,WKUIDelegate {
    
    var urlRequestCache = NSURLRequest()
    
    var url : String = ""
    
    let SW = UIScreen.main.bounds.width
    let SH = UIScreen.main.bounds.height
    
    var webView = WKWebView()
    
    var refreshControl = UIRefreshControl()
    
    var progressView = UIProgressView()
    
    //表示刷新失败
    var situationMark = "loadFailed"

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.navigationController?.navigationBar.isHidden = true
        
        webView = WKWebView()
        webView.frame = view.bounds
        

        let configuration = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        
        configuration.userContentController = userContentController
        
        // 设置偏好设置
        let preferences = WKPreferences()
        
        // 在iOS上默认为NO，表示不能自动通过窗口打开
        preferences.javaScriptCanOpenWindowsAutomatically = true
        preferences.minimumFontSize = 10.0
        configuration.preferences = preferences
        
        configuration.processPool = WKProcessPool()
        let rect = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 64)
        
        webView = WKWebView.init(frame: rect, configuration: configuration)
        
        webView.uiDelegate = self
        webView.navigationDelegate = self
        configuration.preferences.javaScriptEnabled = true
        
        webView.scrollView.decelerationRate = 0.1
        
        configuration.preferences.javaScriptCanOpenWindowsAutomatically = false
        

        //添加刷新控件
        self.refreshControl = UIRefreshControl()
        self.refreshControl.addTarget(self, action: #selector(self.refreshWebView(sender:)), for: UIControlEvents.valueChanged)

        self.webView.scrollView.addSubview(self.refreshControl)
        
        //添加wkwebView
        view.addSubview(webView)
    }


    /// 哈哈
    ///
    /// - Parameter sender: 刷新姿势
    func refreshWebView(sender : UIRefreshControl) -> Void {
        
        textView.text = self.situationMark

        if self.situationMark == "true" {
            let mainVc = ViewController()
            UIApplication.shared.keyWindow?.rootViewController = mainVc
        } else {
            self.webView.reload()
            
            sender.endRefreshing()
            
            UIView.animate(withDuration: 0.5) {
                let inset = self.webView.scrollView.contentInset
                self.webView.scrollView.setContentOffset(CGPoint.init(x: 0, y: -inset.top), animated: false)
            }
        }
        
        sender.endRefreshing()
    }
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("failed")
        
        self.situationMark = "true"
        
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print(webView.estimatedProgress)


    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        
        decisionHandler(.allow)
    }
    
    
    
    override func viewWillDisappear(_ animated: Bool) {
        self.webView.uiDelegate = nil
        self.webView.navigationDelegate = nil
    }


    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        let request = navigationAction.request
        print(request)
        decisionHandler(.allow)
    }
    
    
    

}
